home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_09_04 / 9n04047a < prev    next >
Text File  |  1991-01-14  |  10KB  |  258 lines

  1.  
  2. /*
  3.         NAME = MOUSE.C
  4.         This program demonstrates the fundamentals of using a mouse to
  5.         select options in a menu-driven application.  Uses two "dummy"
  6.         functions: genledg() and payroll().
  7.         Note:  Function chkdrv() taken from page 7-16 of "Microsoft
  8.                Mouse Programmer's Reference Guide"
  9. */
  10.  
  11. #include <stdio.h>
  12. #include <dos.h>                /* required for BIOS calls, etc.        */
  13. #include <string.h>
  14. #include <signal.h>             /* disable & enable control break       */
  15.  
  16. #define RED_BACK 74             /* display using a red background       */
  17.  
  18. extern void pascal far mouse(int*, int*, int*, int*);    /*declare mouse*/
  19.  
  20. void txtcursor(char cur), cls(), outstr(), genledg(), payroll(), heading();
  21. void chkdrv();                  /* checks for existence of mouse driver */
  22.  
  23. int i, handler();               /* handler() part of control-break      */
  24. int m1, m2, m3, m4;             /* mouse parameters                     */
  25. struct menu                     /* structure for our sample menu        */
  26. {
  27.         char text[20];                  /* text of options              */
  28.         int x;                          /* horiz. screen coordinate     */
  29.         int y;                          /* vert. screen coordinate      */
  30.         int mh,mh2,mv;                  /* mouse horiz/vert. coordinates*/
  31. }options[3];                            /* 3 options in our sample prog.*/
  32.  
  33. main()
  34. {
  35.  
  36.         int  choice, loop;
  37.  
  38.                 /* disable Control-Break interrupt      */
  39.  
  40.         if(signal(SIGINT,handler) == (int(*) ())-1)
  41.         {
  42.                 cls();
  43.                 printf("Fatal error - could not disable Control-Break\n");
  44.                 abort();
  45.         }
  46.  
  47.                 /* check for mouse hardware & driver    */
  48.  
  49.         m1=0;
  50.         mouse(&m1,&m2,&m3,&m4);         /* check hardware here  */
  51.         if(m1 != -1)
  52.         {
  53.                 cls();
  54.                 printf("Fatal error - no mouse found..\n");
  55.                 abort();
  56.         }
  57.         chkdrv();                       /* check for mouse driver       */
  58.  
  59.         /*      "Populate" our structure with data      */
  60.  
  61.         strcpy(options[0].text,"General Ledger");
  62.                options[0].x = 6;
  63.                options[0].y = 30;
  64.                options[0].mh = 240;     /* mh & mh2 define the horiz.   */
  65.                options[0].mh2= 344;     /* limits of option's "hot spot"*/
  66.                options[0].mv = 48;      /* define vertical limit here   */
  67.  
  68.         strcpy(options[1].text,"Payroll");
  69.                options[1].x = 8;
  70.                options[1].y = 30;
  71.                options[1].mh = 240;
  72.                options[1].mh2= 288;
  73.                options[1].mv = 64;
  74.  
  75.         strcpy(options[2].text,"Exit");
  76.                options[2].x = 10;
  77.                options[2].y = 30;
  78.                options[2].mh = 240;
  79.                options[2].mh2= 264;
  80.                options[2].mv = 80;
  81.  
  82.         heading();                      /* display menu, etc.   */
  83.         m1=m2=m3=m4=i=0;                /* initialize variables */
  84.         mouse(&m1,&m2,&m3,&m4);         /* init mouse           */
  85.         txtcursor(0);                   /* turn off text cursor */
  86.         m1=1;                           /* show cursor          */
  87.         mouse(&m1,&m2,&m3,&m4);
  88.  
  89.         /*      now set up infinite loop which processes the
  90.                 user's requests                                 */
  91.  
  92.         loop=1;
  93.         while(loop)
  94.         {
  95.                 m1=3;                   /* read mouse cursor position   */
  96.                 m2=m3=m4=0;
  97.                 mouse(&m1,&m2,&m3,&m4);
  98.  
  99.                 for(i=0; i < 3; ++i)    /* is cursor in 1 of 3 options? */
  100.                 {
  101.                    if((m3 >=options[i].mh && m3 <= options[i].mh2) &&
  102.                       (m4 == options[i].mv))
  103.                    {
  104.                         outstr(options[i].x,options[i].y,options[i].text,RED_BACK);
  105.                         break;
  106.                    }
  107.                    else
  108.                         outstr(options[i].x,options[i].y,options[i].text,10);
  109.                 }
  110.  
  111.                 m1=6;                   /* read left button     */
  112.                 m2=m3=m4=0;
  113.                 mouse(&m1,&m2,&m3,&m4);
  114.                 if(m2)                  /* user pressed left button     */
  115.                 {
  116.                         choice= -1;     /* reset from last choice made  */
  117.                         for(i=0; i < 3; i++)
  118.                         {
  119.                                 if((m3 >= options[i].mh && m3 <= options[i].mh2)
  120.                                   && (m4 == options[i].mv))
  121.                                         choice = i;
  122.                         }
  123.                         switch(choice)
  124.                         {
  125.                                 case 0:         /* selected GEN. LEDGER */
  126.                                         genledg();
  127.                                         break;
  128.                                 case 1:         /* selected PAYROLL     */
  129.                                         payroll();
  130.                                         break;
  131.                                 case 2:         /* selected EXIT        */
  132.                                         loop=0; /* ends infinite loop   */
  133.                                         break;
  134.                                 default:        /* meaningless "click"  */
  135.                                         break;
  136.                         }
  137.                         m1=m2=m3=m4=i=0;
  138.                         mouse(&m1,&m2,&m3,&m4);         /* init mouse    */
  139.                         m1=1;                           /* show cursor   */
  140.                         mouse(&m1,&m2,&m3,&m4);
  141.                 }
  142.         }
  143.                 /* perform housekeeping chores before exiting   */
  144.  
  145.         m1=2;                           /* hide mouse cursor    */
  146.         mouse(&m1,&m2,&m3,&m4);
  147.         txtcursor(1);                   /* restore text cursor  */
  148.         cls();                          /* clear screen         */
  149.         signal(SIGINT, SIG_IGN);        /* enable control-break */
  150.         exit(0);                        /* return to DOS        */
  151.  
  152. }                       /*  END MAIN PROGRAM    */
  153.  
  154.  
  155. void txtcursor(char cur)                /* controls text cursor */
  156. {
  157.         union REGS r;                   /* prepare registers,etc*/
  158.         r.h.ah = 1;
  159.         if(cur)                         /* turn text cursor on  */
  160.         {
  161.                 r.h.cl = 7;
  162.                 r.h.ch = 6;
  163.         }
  164.         else
  165.         {
  166.                 r.h.ch = 113;           /* set bits 5,6         */
  167.                 r.h.cl = 0;             /* set lower bits       */
  168.         }
  169.         int86(0x10, &r, &r);            /* execute the interrupt*/
  170. }
  171.  
  172. void cls()                              /* clears the screen    */
  173. {
  174.         union REGS r;
  175.         r.h.ah = 6;                     /* screen scroll code   */
  176.         r.h.al = 0;                     /* clear screen code    */
  177.         r.h.ch = 0;                     /* start row            */
  178.         r.h.cl = 0;                     /* start column         */
  179.         r.h.dh = 24;                    /* end row              */
  180.         r.h.dl = 79;                    /* end column           */
  181.         r.h.bh = 7;
  182.         int86(0x10, &r, &r);            /* execute interrupt    */
  183. }
  184.  
  185. void outstr(x,y,s, color)       /* writes a string to video RAM */
  186. char *s;                               /* string to write       */
  187. char color;                            /* color of string       */
  188. int x,y;                               /* where to start writing*/
  189. {
  190.         char far *vid_mem;             /* video memory pointer  */
  191.         vid_mem= (char far *) 0xB8000000;       /* start of video memory */
  192.         vid_mem=vid_mem+((x * 160) + (y * 2));  /* where to start output */
  193.         while(*s)                               /* loop thru string...   */
  194.         {
  195.  
  196.                 *vid_mem=*s++;          /* move string into video memory*/
  197.